package guyubao.com.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import guyubao.com.controller.BaseController;
import guyubao.com.entity.RestBean;
import guyubao.com.entity.dto.AccountDto;
import guyubao.com.service.AccountService;
import guyubao.com.service.RedisService;
import guyubao.com.utils.Const;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Validated
@RestController
@RequestMapping("/api/admin/account")
@Tag(name = "用户 相关", description = "获取用户 封禁 解封")
public class AdminAccountController extends BaseController {
    @Resource
    AccountService accountService;
    @Resource
    RedisService redisService;
    @Operation(summary ="获取所有用户信息")
    @GetMapping("/all")
    public RestBean<List<AccountDto>> getAllAccount(@RequestAttribute String role) {
        if (!isAdmin(role)) return RestBean.forbidden("您不是管理员");
        return RestBean.success(accountService.getAllAccount());
    }
    @Operation(summary ="获取所有用户信息以分页返回")
    @GetMapping("/allPage")
    public RestBean<Page<AccountDto>> getAllAccountPage(@RequestParam Long size,
                                                        @RequestParam Long current,
                                                        @RequestParam String column,
                                                        @RequestParam String val,
                                                        @RequestAttribute String role) {
        if (!isAdmin(role)) return RestBean.forbidden("您不是管理员");
        return RestBean.success(accountService.getAllAccpuntPage(size,current,column,val));
    }
    @Operation(summary ="根据用户名或邮箱查询用户信息")
    @GetMapping("/id")
    public RestBean<AccountDto> findAccountByNameOrEmail(@RequestParam String text,
                                                         @RequestAttribute String role){
        if(!isAdmin(role))return RestBean.forbidden("您不是管理员");
        return RestBean.success(accountService.findAccountByNameOrEmail(text));
    }
    @Operation(summary ="根据id或用户名或邮箱封禁用户(小时)")
    @PutMapping("/disabled")
    public RestBean<Void> disableAccount(@RequestParam String id,
                                         @RequestParam int hour,
                                         @RequestAttribute String role){
        if(!isAdmin(role))return RestBean.forbidden("您不是管理员");
        if (accountService.findAccountById(id)==null){
            AccountDto accountByNameOrEmail = accountService.findAccountByNameOrEmail(id);
            id = accountByNameOrEmail.getId();
        }
        String s = accountService.disableAccount(id, hour);
        if (s!=null) return RestBean.failure(s);
        return RestBean.success();
    }
    @Operation(summary ="根据id或用户名或邮箱解封")
    @PutMapping("/able")
    public RestBean<Void> ableAccount(@RequestParam String id,
                                      @RequestAttribute String role){
        if(!isAdmin(role))return RestBean.forbidden("您不是管理员");
        if (accountService.findAccountById(id)==null){
            AccountDto accountByNameOrEmail = accountService.findAccountByNameOrEmail(id);
            id = accountByNameOrEmail.getId();
        }
        String s = accountService.ableAccount(id);
        if (s!=null) return RestBean.failure(s);
        return RestBean.success();
    }
    @Operation(summary ="根据id或用户名或邮箱删除用户")
    @DeleteMapping("/del")
    public RestBean<Void> delAccount(@RequestParam String id,
                                      @RequestAttribute String role){
        if(!isAdmin(role))return RestBean.forbidden("您不是管理员");
        if (accountService.findAccountById(id)==null){
            AccountDto accountByNameOrEmail = accountService.findAccountByNameOrEmail(id);
            id = accountByNameOrEmail.getId();
        }
        // 执行删除用户
        String s = accountService.deleteOneself(id);
        // 删除redis在线用户凭证
        if (s==null)redisService.delKey(Const.JWT_WHITE_LIST + id);
        return this.messageHandle(()-> s);
    }
}
